package com.ktv.web.controller.com.ktv.web.handler.impl;

import com.ktv.web.controller.com.ktv.web.handler.RequestHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Created by ivey.yuan on 2016/3/2.
 */
@Component
public class SteamRequestHandler extends AbstractRequestHandler implements RequestHandler {

    @Override
    public String handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
        super.startAsyncRequest(request, response);

        StringBuilder sb;
        for (; ; ) {
            sb = new StringBuilder();
            sb.append("<script> ")
                    .append(" var parentDocument = parent.document ;")
                    .append(" var currentTime = parentDocument.getElementById(\"currentTimeSteam\").innerHTML = \"");
            sb.append(new Date().toString()).append("\"; ")
                    .append("</script>");
            super.writerResponse(response, sb.toString() + "\n","text/html;charset=GBK");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
